package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.SearchModel;
import com.ruitu.health_2.mvp.presenter.SearchPresenter;
import com.ruitu.health_2.ui.adapter.ModelArtManListAdapter;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.health_2.ui.adapter.SearchVideoListAdapter;
import com.ruitu.health_2.ui.adapter.SerachProListAdapter;
import com.ruitu.health_2.ui.common.ArticleListActivity;
import com.ruitu.health_2.ui.common.ProductListActivity;
import com.ruitu.health_2.ui.common.VideoDetailActivity;
import com.ruitu.health_2.ui.common.VideoListActivity;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.home.SearchData;
import com.ruitu.router_module.bean.product.ArtMan;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Video;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 搜索页面
public class SearchActivity extends BaseActivity<SearchPresenter.SearchListP, SearchModel.SearchListM> {
    private ImageView iv_back;// 返回
    private TextView tv_search;// 搜索
    private EditText et_name;// 输入框
    private LinearLayout ll_1, ll_2, ll_3, ll_4;// 标题加列表的布局
    private RecyclerView rcv_list_1, rcv_list_2, rcv_list_3, rcv_list_4;// 搜索出来的数据列表
    private LinearLayout ll_data;// 搜索出来的数据
    private TextView tv_empty;
    private TextView tv_more_1, tv_more_2, tv_more_3, tv_more_4;

    private ModelArtManListAdapter artManAdapter;// 艺人
    private SearchVideoListAdapter videoAdapter;// 视频
    private ModelArticleListAdapter articleAdapter;// 文章
    private SerachProListAdapter proAdapter;// 商品

    private String currName;// 记录当前搜索的关键字

    // 请求得到的数据
    private List<ArtMan> artManList = new ArrayList<>();
    private List<Video> videoList = new ArrayList<>();
    private List<Article> articleList = new ArrayList<>();
    private List<ProHome> proList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_search;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_back = findViewById(R.id.iv_back);
        tv_search = findViewById(R.id.tv_search);
        et_name = findViewById(R.id.et_name);
        ll_1 = findViewById(R.id.ll_1);
        ll_2 = findViewById(R.id.ll_2);
        ll_3 = findViewById(R.id.ll_3);
        ll_4 = findViewById(R.id.ll_4);
        rcv_list_1 = findViewById(R.id.rcv_list_1);
        rcv_list_2 = findViewById(R.id.rcv_list_2);
        rcv_list_3 = findViewById(R.id.rcv_list_3);
        rcv_list_4 = findViewById(R.id.rcv_list_4);
        ll_data = findViewById(R.id.ll_data);
        tv_empty = findViewById(R.id.tv_empty);
        tv_more_1 = findViewById(R.id.tv_more_1);
        tv_more_2 = findViewById(R.id.tv_more_2);
        tv_more_3 = findViewById(R.id.tv_more_3);
        tv_more_4 = findViewById(R.id.tv_more_4);

        // 艺人
        LinearLayoutManager manager = new LinearLayoutManager(this);
        artManAdapter = new ModelArtManListAdapter(this);
        rcv_list_1.setLayoutManager(manager);
        rcv_list_1.setAdapter(artManAdapter);
        // 视频
        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        videoAdapter = new SearchVideoListAdapter(this);
        rcv_list_2.setLayoutManager(manager2);
        rcv_list_2.setAdapter(videoAdapter);
        // 文章
        LinearLayoutManager manager3 = new LinearLayoutManager(this);
        articleAdapter = new ModelArticleListAdapter(this);
        rcv_list_3.setLayoutManager(manager3);
        rcv_list_3.setAdapter(articleAdapter);
        // 商品
        LinearLayoutManager manager4 = new LinearLayoutManager(this);
        proAdapter = new SerachProListAdapter(this);
        rcv_list_4.setLayoutManager(manager4);
        rcv_list_4.setAdapter(proAdapter);

        ll_data.setVisibility(View.GONE);
        tv_empty.setVisibility(View.VISIBLE);

        setListeners();
    }

    private void setListeners() {
        setOnClickListener(iv_back, tv_search, tv_more_1, tv_more_2, tv_more_3, tv_more_4);

        // 点击艺人查看详情
        artManAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                // 查看艺人详情
                Bundle bundle = new Bundle();
                bundle.putString("id", artManList.get(position).getId());
                startActivity(ArtManDetailActivity.class, bundle);
            }
        });
        // 点击视频查看详情
        videoAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("videoId", videoList.get(position).getId());
                bundle.putString("videoUrl", videoList.get(position).getVideo());
                startActivity(VideoDetailActivity.class, bundle);
            }
        });
        // 点击查看文章详情
        articleAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(SearchActivity.this, "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        // 点击查看商品详情
        proAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", proList.get(position).getId());
                startActivity(ProductDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 搜索完成
            SearchData searchData = (SearchData) data;

            artManList = searchData.getUmsArtManList();
            videoList = searchData.getCmsVideoList();
            articleList = searchData.getCmsArticleList();
            proList = searchData.getPmsProductList();

            artManAdapter.setDataList(artManList);
            videoAdapter.setDataList(videoList);
            articleAdapter.setDataList(articleList);
            proAdapter.setDataList(proList);

            setViews();
        }
    }

    // 根据数据设置页面
    private void setViews() {
        if (isDataEmpty(artManList)) {
            ll_1.setVisibility(View.GONE);
            tv_more_1.setVisibility(View.GONE);
        } else {
            ll_1.setVisibility(View.VISIBLE);
            if (artManList.size() >= 3) {
                tv_more_1.setVisibility(View.VISIBLE);
            } else {
                tv_more_1.setVisibility(View.GONE);
            }
        }
        if (isDataEmpty(videoList)) {
            ll_2.setVisibility(View.GONE);
            tv_more_2.setVisibility(View.GONE);
        } else {
            ll_2.setVisibility(View.VISIBLE);
            if (videoList.size() >= 3) {
                tv_more_2.setVisibility(View.VISIBLE);
            } else {
                tv_more_2.setVisibility(View.GONE);
            }
        }
        if (isDataEmpty(articleList)) {
            ll_3.setVisibility(View.GONE);
            tv_more_3.setVisibility(View.GONE);
        } else {
            ll_3.setVisibility(View.VISIBLE);
            if (articleList.size() >= 3) {
                tv_more_3.setVisibility(View.VISIBLE);
            } else {
                tv_more_3.setVisibility(View.GONE);
            }
        }
        if (isDataEmpty(proList)) {
            ll_4.setVisibility(View.GONE);
            tv_more_4.setVisibility(View.GONE);
        } else {
            ll_4.setVisibility(View.VISIBLE);
            if (proList.size() >= 3) {
                tv_more_4.setVisibility(View.VISIBLE);
            } else {
                tv_more_4.setVisibility(View.GONE);
            }
        }

        if (ll_1.getVisibility() == View.GONE && ll_2.getVisibility() == View.GONE
                && ll_3.getVisibility() == View.GONE && ll_4.getVisibility() == View.GONE) {
            tv_empty.setVisibility(View.VISIBLE);
            ll_data.setVisibility(View.GONE);
        } else {
            tv_empty.setVisibility(View.GONE);
            ll_data.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == iv_back) {
            finish();
        }
        if (v == tv_search) {// 搜索
            String name = et_name.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShortSafe("请输入关键字");
                return;
            }
            currName = name;
            rxList.add(p.reqSearch(currName));
            hideProgress();
            showProgressWithText(true, "搜索中..");
        }
        if (v == tv_more_1) {// 更多艺人
            Bundle bundle = new Bundle();
            bundle.putString("name", currName);
            startActivity(ArtManListActivity.class, bundle);
        }
        if (v == tv_more_2) {// 更多视频
            Bundle bundle = new Bundle();
            bundle.putString("name", currName);
            startActivity(VideoListActivity.class, bundle);
        }
        if (v == tv_more_3) {// 更多文章
            Bundle bundle = new Bundle();
            bundle.putString("name", currName);
            startActivity(ArticleListActivity.class, bundle);
        }
        if (v == tv_more_4) {// 更多商品
            Bundle bundle = new Bundle();
            bundle.putString("name", currName);
            startActivity(ProductListActivity.class, bundle);
        }
    }
}
